//
//  YMPickerSelectView.m
//  scale
//
//  Created by 李钰 on 16/9/7.
//  Copyright © 2016年 李钰. All rights reserved.
//

#import "YMPickerSelectView.h"
@interface YMPickerSelectView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong)UIView * topView;//关闭 取消

@end
@implementation YMPickerSelectView
{
    NSArray        * _source;//数据源 @[@[str],@[str]]
    NSMutableArray * _selectIndexs;//pickerView选中的坐标
    NSMutableArray * _pickerViews;//pickerView对象
    CGFloat          _rowHeight;
}
#pragma mark - 力量类型
- (id)initWithDataParentSource:(NSArray *)source AndRowHeight:(CGFloat)rowHeight
{
    _rowHeight = rowHeight;
    _source = [source copy];
    if ([super initWithFrame: CGRectMake(0, ScreenHeight, ScreenWidth, 320)]) {
        self.backgroundColor = [UIColor whiteColor];
        [[UIApplication sharedApplication].keyWindow addSubview: self];
        [self addSubview: self.topView];
        [self loadPickerViews];
    }
    return self;
}
- (void)loadPickerViews
{
    CGFloat width = (ScreenWidth-60.f)/_source.count;
    _selectIndexs  = [NSMutableArray new];
    _pickerViews    = [NSMutableArray new];
    for(NSInteger i = 0;i< _source.count;i++){
        UIPickerView * picker = [[UIPickerView alloc]initWithFrame: CGRectMake(i*width+30, self.topView.bottom, width, self.height - self.topView.bottom)];
        [self addSubview: picker];
        picker.delegate = self;
        picker.dataSource = self;
        picker.tag = i;
        NSArray * source = _source[i];
        NSInteger index = source.count/2;
        [picker selectRow: index inComponent:0 animated: NO];
        [_selectIndexs addObject: @(index)];
        [_pickerViews addObject: picker];
    }
}
#pragma mark -UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSArray * source = _source[pickerView.tag];
    return source.count;
}
#pragma mark - UIPickerViewDelegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    [_selectIndexs replaceObjectAtIndex: pickerView.tag withObject: @(row)];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return _rowHeight;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel) {
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.font = [UIFont systemFontOfSize: 16];
        pickerLabel.textColor = RGB(50, 50, 50);
        pickerLabel.textAlignment = NSTextAlignmentCenter;
    }
    pickerLabel.text = _source[pickerView.tag][row];
    if (_selectIndexs && pickerView.tag<_selectIndexs.count) {
        [_selectIndexs replaceObjectAtIndex: pickerView.tag withObject: @(row)];
    }
    return pickerLabel;
}

#pragma mark -UIButton
- (void)onCancel:(id)sender
{
    [self closeAnimation];
}
- (void)onSubmit:(id)sender
{
    [self closeAnimation];
    if (self.delegate && [self.delegate respondsToSelector: @selector(pickerViewSelectSource:)]) {
        [self.delegate pickerViewSelectSource: _selectIndexs];
    }
    if (self.delegate && [self.delegate respondsToSelector: @selector(pickerViewSelectSource:AndModel:)]) {
        NSMutableArray * selectSource = [NSMutableArray new];
        for (NSInteger i=0; i<_selectIndexs.count; i++) {
            NSArray * arr = _source[i];
            NSInteger index = [_selectIndexs[i] integerValue];
            [selectSource addObject: arr[index]];
        }
        [self.delegate pickerViewSelectSource: selectSource AndModel: self.model];
    }
}
#pragma 懒加载
- (UIView *)topView
{
    if (nil == _topView) {
        _topView = [[UIView alloc]initWithFrame: CGRectMake(0, 0, ScreenWidth, 50.f)];
        _topView.backgroundColor = RGB(243, 243, 247);
        //关闭
        UIButton * leftBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        [_topView addSubview: leftBtn];
        leftBtn.size = CGSizeMake(50, 50.f);
        [leftBtn setImage: [UIImage imageNamed: @"close_icon"] forState: UIControlStateNormal];
        [leftBtn setImage: [UIImage imageNamed: @"close_icon"] forState: UIControlStateHighlighted];
        [leftBtn addTarget: self action: @selector(onCancel:) forControlEvents: UIControlEventTouchUpInside];
        //提交
        UIButton * rightBtn = [UIButton buttonWithType: UIButtonTypeCustom];
        [_topView addSubview: rightBtn];
        rightBtn.frame = CGRectMake(ScreenWidth - 50, 0, 50, 50.f);
        [rightBtn setImage: [UIImage imageNamed: @"yes_icon"] forState: UIControlStateNormal];
        [rightBtn setImage: [UIImage imageNamed: @"yes_icon"] forState: UIControlStateHighlighted];
        [rightBtn addTarget: self action: @selector(onSubmit:) forControlEvents: UIControlEventTouchUpInside];
    }
    return _topView;
}
- (UILabel *)labelTitle
{
    if (nil == _labelTitle) {
        _labelTitle = [[UILabel alloc]initWithFrame: CGRectMake(60.f, 0, ScreenWidth - 120.f, self.topView.height)];
        [self.topView addSubview: _labelTitle];
        _labelTitle.font = [UIFont systemFontOfSize: 18.f];
        _labelTitle.textColor = [UIColor blackColor];
        _labelTitle.textAlignment = NSTextAlignmentCenter;
    }
    return _labelTitle;
}
- (UIView *)blackBg
{
    if (nil == _blackBg) {
        _blackBg = [[UIView alloc]initWithFrame: CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _blackBg.alpha = 0.f;
        _blackBg.backgroundColor = RGBA(0, 0, 0, 0.8f);
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget: self action: @selector(closeAnimation)];
        [_blackBg addGestureRecognizer: tap];
    }
    return _blackBg;
}
#pragma mark -setter
- (void)setBlackScreen:(BOOL)blackScreen
{
    _blackScreen = blackScreen;
    if (blackScreen) {
        [[UIApplication sharedApplication].keyWindow addSubview: self.blackBg];
        [[UIApplication sharedApplication].keyWindow addSubview: self];
    }
}
- (void)setModel:(id)model
{
    _model = model;
}
//退场动画
- (void)closeAnimation
{
    if (self.blackScreen) {
        [UIView animateWithDuration: 0.4f animations:^{
            self.blackBg.alpha = 0;
        } completion:^(BOOL finished) {
            [self.blackBg removeFromSuperview];
        }];
    }
    [UIView animateWithDuration: 0.4f animations:^{
        self.top = ScreenHeight;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)showCustomLineWithColor:(UIColor *)color
{
    CGFloat top = 0.f;
    CGFloat bottom = 0.f;
    for (UIPickerView * pickerView in _pickerViews) {
        UIView * topView = [pickerView.subviews objectAtIndex:1];
        UIView * bottomView = [pickerView.subviews objectAtIndex:2];
        top = topView.top;
        bottom = bottomView.top;
        topView.hidden = YES;
        bottomView.hidden = YES;
//        [[pickerView.subviews objectAtIndex:1] setHidden:TRUE];
//        [[pickerView.subviews objectAtIndex:2] setHidden:TRUE];
    }
    top = top + _rowHeight;
    //(pickerHeight - _rowHeight)/2 + self.topView.bottom
//    CGFloat pickerHeight = self.height - self.topView.bottom;
    UIView * topLine = [[UIView alloc]initWithFrame: CGRectMake(30.f, top, ScreenWidth - 60.f, 2.f)];
    [self addSubview: topLine];
    topLine.backgroundColor = color;
    topLine.layer.cornerRadius = topLine.height/2;
    UIView * bottomLine = [[UIView alloc]initWithFrame:topLine.frame];
    [self addSubview: bottomLine];
    bottomLine.top = topLine.top + _rowHeight - bottomLine.height;
    bottomLine.backgroundColor = color;
    bottomLine.layer.cornerRadius = topLine.height/2;
}
- (void)show
{
    if (_blackScreen) {
        [UIView animateWithDuration: 0.4f animations:^{
            self.blackBg.alpha = 1;
        }];
    }
    
    self.top = ScreenHeight;
    [UIView animateWithDuration: 0.4f animations:^{
        self.top = ScreenHeight - self.height;
    }];
}
- (void)setSelectIndexs:(NSArray *)selectIndexs
{
    if (selectIndexs.count == _pickerViews.count) {
        for(NSInteger i = 0;i<selectIndexs.count;i++){
            NSInteger index = [selectIndexs[i] integerValue];
            UIPickerView * picker = _pickerViews[i];
            [picker selectRow: index inComponent:0 animated: NO];
            [_selectIndexs replaceObjectAtIndex: i withObject: @(index)];
        }
    }
}
@end
